// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import axios from './lib/service/index.js'

import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import Vant from 'vant';
import 'vant/lib/index.css';
import md5 from 'js-md5';
import './lib/StringExtend.js'
import Filters from './lib/Filters';
import store from './lib/store/Store.js'

Vue.use(Vuex);
Vue.use(Filters);
Vue.use(MintUI);
Vue.use(Vant);
Vue.prototype.$axios = axios;
Vue.prototype.$md5 = md5;
Vue.config.productionTip = false;

import utils from './lib/StringExtend'
/* eslint-disable no-new */
// router.beforeEach((to, from, next) => {
//   let timestamp = (new Date()).valueOf();
//   let url = window.location.href;
//   if (window.location.href.indexOf('?') >= 0) {
//     if (window.location.href.indexOf('t=') >= 0) {
//       return;
//     }
//     window.location.href = url + '&t=' + timestamp;
//   } else {
//     if (window.location.href.indexOf('t=') >= 0) {
//       return;
//     }
//     window.location.href = url + '?t=' + timestamp;
//     console.log(url + '?t=' + timestamp)
//   }
//   next();
// });
new Vue({
  el: '#app',
  router,
  store,
  data() {
    return {
      appid: utils.GetQueryString('appid'),
      code: utils.GetQueryString('code'),
      wechatId: utils.getCookie('wechat_id'),
      wechat_id: this.$route.query.wechat_id,//公众号
    }
  },
  methods: {
    init() {
      if (utils.isNone(utils.getCookie('wechat_id_' + this.wechatId)) || utils.isNone(utils.getCookie('open_id_' + this.wechatId)) && utils.isNone(this.code)) {
        this.getWechat();
      }
      if (!utils.isNone(this.code) && !utils.isNone(this.appid) && utils.isNone(utils.getCookie('open_id_' + this.wechatId))) {
        this.userInfo();
      }
    },
    //微信授权
    getWechat() {
      this.wechatId = utils.getCookie('wechat_id');
      utils.setCookie('wechat_id_' + this.wechatId, this.wechatId);
      let params = {
        redirect_url: window.location.href,
        wechat_id: this.wechatId
      };
      this.$axios.wechatWechatOauth(params).then(res => {
        if (res.errcode !== '000') {
          this.$toast(res.errmsg);
          return false;
        }
        let url = res.data.url;
        window.location.href = url;
      })
    },
    //获取用户信息
    userInfo() {
      let params = {
        appid: this.appid,
        code: this.code,
      };
      this.$axios.wechatGetWechatUser(params).then(res => {
        if (res.errcode !== '000') {
          this.$dialog.alert({
            message: res.errmsg,
          });
          return false;
        }
        utils.setCookie('open_id_' + this.wechatId, res.data[0].open_id);
        window.location.reload();
      })
    },

  },

  watch: {
    $route(to, from) {
      if (!utils.isNone(this.$route.query.wechat_id)) {
        utils.setCookie('wechat_id', this.$route.query.wechat_id);
        this.wechatId = utils.getCookie('wechat_id');
      }
      if (to.path !== '/' && to.path !== '/lobby') {
        this.init();
      }
    },
  },
  components: {App},
  template: '<App/>'
});
